<?php
/**
 * 使用示例
 *
 * @author M2
 * @version 1.0
 * create Date 2013-11-1
 * update Date 2013-11-4
 * update Date 2013-11-5
 * update Date 2013-11-6
 * update Date 2013-11-7
 * <yl:a href="http://www.souhu.com" target="_blank">测试</yl:a>
 * <yl:a channelName="开发者社区" target="_blank" title="开发者社区3">开发者社区2</yl:a>
 * <yl:a><yl:content type="title"></yl:content></yl:a>
 * 配合其他标签一起使用
 */
class YlATaglib {
    public function __construct() {
        C ( include SITE_PATH . '/extends/taglib/tagarr.php' );
    }
    // channelname,target,href,title,class,rel,style
    public function parse($content, $sid, $arr = array(),$type='1',$domain,$channel) {
        if (stripos($content, 'yl:a') !== false) {
            $matches = preg_parse($content, 'yl:a');
            foreach ($matches as $match) {
                $attrString = getParseAttrString($match, 'yl:a');
                $href = getParseOneAttr($attrString, 'href');
                $title = getParseOneAttr($attrString, 'title');
                $channelName = getParseOneAttr($attrString, 'channelname');
                $target = getParseOneAttr($attrString, 'target');
                $class = getParseOneAttr($attrString, 'class');
                $style = getParseOneAttr($attrString, 'style');
                $innertext = getParseContentText($match, 'yl:a');
                //当$channelName 不为空时选择有$channelname 的链接
                if (!empty($arr)&&empty($channelName)) {
                    if ($type === '1') {
                        if (empty($href)) {
                            $href = $arr['url'];
                        }
                        if (empty($title)) {
                            $title = $arr['title'];
                        }
                        if (empty($innertext)) {
                            $innertext = $arr['title'];
                        }
                    } elseif ($type === '2') {
                        if (empty($href)) {
                            $href = $arr['url'];
                        }
                        if (empty($title)) {
                            $title = $arr['cname'];
                        }
                        if (empty($innertext)) {
                            $innertext = $arr['cname'];
                        }
                    } elseif ($type === '4') {
                        if (empty($href)) {
                            $href = $arr['cateurl'];
                        }
                        if (empty($title)) {
                            $title = $arr['cname'];
                        }
                        if (empty($innertext)) {
                            $innertext = $arr['cname'];
                        }
                    }
                }
                if (! empty ( $channelName )) {
                    //add next 2013-12-11 增加单栏目标签缓存
                    if (($findChannel = S('cache_channelname_'.$sid.'_'.$channelName)) === false) {
                        $findChannel = M('cms_category')
                            ->field('(case when url <> "" then url when createurl <> "" then createurl
	  else (concat("'.$domain.'","/",replace(replace((case when catenaming <> ""
	                		 then catenaming else ("'.$channel['catenaming'].'") end),"{cateid}",id),"{@ChannelID}",id))) end) as url,
	  replace(imageurl,"@","'.$domain.'") as imageurl,id,cname,content,keywords,description')
                            ->where('siteid='.$sid.' and cname="'.$channelName.'"')
                            ->find();
                        S('cache_channelname_'.$sid.'_'.$channelName, $findChannel);
                    }
                    if(empty($href)) {
                        $href = $findChannel['url'];
                    }
                    if (empty($title)) {
                        $title = $findChannel['cname'];
                    }
                    if (empty($innertext)) {
                        $innertext = $findChannel['cname'];
                    }
                }
                $parseStr = '<a href="' . $href . '" ';
                if (! empty ( $title ))
                    $parseStr .= 'title="' . $title . '" ';
                if (! empty ( $target ))
                    $parseStr .= 'target="' . $target . '" ';
                if (! empty ( $class ))
                    $parseStr .= 'class="' . $class . '" ';
                if (! empty ( $style ))
                    $parseStr .= 'style="' . $style . '" ';
                $parseStr .= '>' . $innertext . '</a>';
                $content = str_replace($match, $parseStr, $content);
            }
        }
        return $content;
    }
}